package com.example.handler;

import cn.hutool.core.codec.Base64;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.TypeHandler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@MappedJdbcTypes(JdbcType.VARCHAR)
public class StringHandler implements TypeHandler<String> {
    @Override
    public void setParameter(PreparedStatement preparedStatement, int i, String s, JdbcType jdbcType) throws SQLException {
        String encryption = Base64.encode(s);
        System.out.println("------------------------setParameter------------------------"+ encryption + "===" + s);
        preparedStatement.setString(i, encryption);
    }

    @Override
    public String getResult(ResultSet resultSet, String s) throws SQLException {
        String encryption  = resultSet.getString(s);
        System.out.println("------------------------getResult------------------------"+ encryption + "===" + s);
        return Base64.decodeStr(encryption);
    }

    @Override
    public String getResult(ResultSet resultSet, int i) throws SQLException {
        String encryption  = resultSet.getString(i);
        System.out.println("------------------------getResult------------------------"+ encryption + "===" + i);
        return Base64.decodeStr(encryption);
    }

    @Override
    public String getResult(CallableStatement callableStatement, int i) throws SQLException {
        String encryption = callableStatement.getString(i);
        System.out.println("------------------------getResult------------------------"+ encryption + "===" + i);
        return Base64.decodeStr(encryption);
    }
}
